package com.zhudao.crawler.service;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CrawlerDoubanService {

    @Autowired
    @Qualifier("primaryJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    private static final Logger logger = LoggerFactory.getLogger(CrawlerDoubanService.class);
    private static Pattern pattern = Pattern.compile("1\\d{10}");

    @Async
    public void douban() throws IOException, InterruptedException {
        while (true) {
            for (int i = 0; i < 430325; i = i + 25) {
                try {
                    URL url = new URL("https://www.douban.com/group/shanghaizufang/discussion?start=" + i);
                    Document document = Jsoup.parse(url, 10000);
                    Elements elements = document.select(".title a");
                    for (Element element : elements) {
                        try {
                            Thread.sleep(10000);
                            String href = element.attr("href");
                            logger.info(href);
                            URL url1 = new URL(href);
                            Document document1 = Jsoup.parse(url1, 10000);
                            String content = document1.select("#link-report").text();
                            Matcher matcher = pattern.matcher(content);
                            if (matcher.find()) {
                                String phone = matcher.group(0);
                                Map<String, Object> map1 = jdbcTemplate.queryForMap("SELECT COUNT(1) as count from userInfo where  phone=?", phone);
                                Integer count = Integer.valueOf(map1.get("count").toString());
                                if (count == 0) {
                                    logger.info("插入豆瓣的数据，电话号码:{}", phone);
                                    jdbcTemplate.update("INSERT into userInfo VALUE (?,?,?,?,?,?,?)", phone, "豆瓣", "房东", "上海", "", "豆瓣", new Date());
                                } else {
                                    logger.info("豆瓣的数据，电话号码已存在:{}", phone);

                                }
                            }
                        } catch (Exception ex) {
                            logger.info("异常了，" + ex.getMessage());
                        }
                    }
                } catch (Exception ex) {
                    logger.info("异常了，" + ex.getMessage());
                }
            }
        }
    }
}


